package com.game.modular.game.battle.panel.skill;

import com.game.comment.util.RedisUtils;
import com.game.modular.game.battle.panel.Battle;
import com.game.modular.game.battle.panel.skill.domain.SkillPanelInfo;
import com.game.modular.game.playerAssets.domain.PlayerHero;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 技能面板信息
 */
@Component
@SuppressWarnings("ALL")
public class PanelInfoService {
    private static final String roundInfoValue_key1 = "battle:%d:panel:roundInfo:skill";
    private static final String battleInfoValue_key1 = "battle:%d:panel:battleInfo:skill";
    private static final String SkillPanel_key2 = "武将:%d:星阶:%d";

    @Resource
    private RedisUtils redisUtils;

    public void init(Long battleId, List<PlayerHero> playerHeroList) {
        for (PlayerHero playerHero : playerHeroList) {
            init(battleId, playerHero);
        }
    }

    private void init(Long battleId, PlayerHero playerHero) {
        Long playerHeroId = playerHero.getId();
        Long skill = playerHero.getSkill();
        for (Long skillStarId : playerHero.getSkillStarIds()) {
            if (skillStarId == skill) {
                SkillPanelInfo battleInfoValueItem = SkillPanelInfo.init();
                battleInfoValueItem.setIsATalent(1);
                redisUtils.hPut(
                        String.format(battleInfoValue_key1, battleId),
                        String.format(SkillPanel_key2, playerHeroId, skillStarId),
                        battleInfoValueItem, 20, TimeUnit.MINUTES
                );
                Map<Integer, SkillPanelInfo> roundInfoValue = new HashMap<>();
                for (Integer i = 0; i < 8; i++) {
                    SkillPanelInfo roundInfoValueItem = SkillPanelInfo.init();
                    battleInfoValueItem.setIsATalent(1);
                    roundInfoValue.put(i, roundInfoValueItem);
                }
                redisUtils.hPut(
                        String.format(roundInfoValue_key1, battleId),
                        String.format(SkillPanel_key2, playerHeroId, skillStarId),
                        roundInfoValue, 20, TimeUnit.MINUTES
                );
            } else {
                SkillPanelInfo battleInfoValueItem = SkillPanelInfo.init();
                battleInfoValueItem.setIsATalent(0);
                redisUtils.hPut(
                        String.format(battleInfoValue_key1, battleId),
                        String.format(SkillPanel_key2, playerHeroId, skillStarId),
                        battleInfoValueItem, 20, TimeUnit.MINUTES
                );
                Map<Integer, SkillPanelInfo> roundInfoValue = new HashMap<>();
                for (Integer i = 0; i < 8; i++) {
                    SkillPanelInfo roundInfoValueItem = SkillPanelInfo.init();
                    battleInfoValueItem.setIsATalent(1);
                    roundInfoValue.put(i, roundInfoValueItem);
                }
                redisUtils.hPut(
                        String.format(roundInfoValue_key1, battleId),
                        String.format(SkillPanel_key2, playerHeroId, skillStarId),
                        roundInfoValue, 20, TimeUnit.MINUTES
                );
            }
        }
    }

    /**
     * 记录技能释放次数
     */
    public void addReleaseFrequency(Long battleId, Long playerHeroId, Long coreSkillStarId) {
        SkillPanelInfo battleInfoValue = (SkillPanelInfo) redisUtils.hGet(
                String.format(battleInfoValue_key1, battleId),
                String.format(SkillPanel_key2, playerHeroId, coreSkillStarId)
        );
        battleInfoValue.addNumber(1);
        redisUtils.hPut(
                String.format(battleInfoValue_key1, battleId),
                String.format(SkillPanel_key2, playerHeroId, coreSkillStarId),
                battleInfoValue, 20, TimeUnit.MINUTES
        );

        Battle battle = Battle.get(battleId);
        Integer round = battle.getRound();
        Map<Object, SkillPanelInfo> roundInfoValue = (Map<Object, SkillPanelInfo>) redisUtils.hGet(
                String.format(roundInfoValue_key1, battleId),
                String.format(SkillPanel_key2, playerHeroId, coreSkillStarId)
        );
        roundInfoValue.forEach((k, v)->{
            v.addNumber(1);
            roundInfoValue.put(k, v);
        });
        redisUtils.hPut(
                String.format(roundInfoValue_key1, battleId),
                String.format(SkillPanel_key2, playerHeroId, coreSkillStarId),
                roundInfoValue, 20, TimeUnit.MINUTES
        );
    }

    public SkillPanelInfo selectBattleInfo(Long battleId, Long playerHeroId, Long coreSkillStarId) {
        return (SkillPanelInfo) redisUtils.hGet(
                String.format(battleInfoValue_key1, battleId),
                String.format(SkillPanel_key2, playerHeroId, coreSkillStarId)
        );
    }

    public SkillPanelInfo selectRoundInfo(Long battleId, Long playerHeroId, Long coreSkillStarId) {
        Battle battle = Battle.get(battleId);
        Integer round = battle.getRound();
        Map<Object, SkillPanelInfo> roundInfoValue = (Map<Object, SkillPanelInfo>) redisUtils.hGet(
                String.format(roundInfoValue_key1, battleId),
                String.format(SkillPanel_key2, playerHeroId, coreSkillStarId)
        );
            return roundInfoValue.get(round.toString());
    }

    public Map<Object, Object> mapBattleInfo(Long battleId) {
        return redisUtils.hGetAll(String.format(battleInfoValue_key1, battleId));
    }

    public Map<Object, Object> mapRoundInfo(Long battleId) {
        return redisUtils.hGetAll(String.format(roundInfoValue_key1, battleId));
    }
}
